//神经网络模拟
const Koa = require('koa');
const Router = require('koa-router');
const { runtime } = require('webpack');
const app = new Koa();
const router = new Router();
app.use(router.routes());

let interval
router.get('/start', async (ctx) => {
    init();
    interval = setInterval(function () {
        play();
    }, 1000);
    ctx.body = '开始';
})
router.get('/stop', async (ctx) => {
    clearInterval(interval)
    ctx.body = '暂停'
})

const nodeCount = 1000;
const connectRate = 0.1;

let nodes = [];
let lines = [];
let linesMap = {};
function init() {
    for (let i = 0; i < nodeCount; i++) {
        nodes.push({
            id: i,
            energy: 100
        })
    }
    for (let node of nodes) {
        //随机找另外几个node连接,方便查找，连接的id设置为 小id+,+大id
        for (let i = 0; i < nodeCount * connectRate; i++) {
            let nodeIndex = Math.round(Math.random() * nodeCount);
            let aimNode = nodes[nodeIndex];
            let startId = node.id;
            let endId = aimNode.id;
            if (startId > endId) {
                startId = aimNode.id;
                endId = node.id;
            }
            let lineId = startId + "," + endId;
            if (typeof linesMap[lineId] === 'undefined') {
                let line = {
                    id: lineId,
                    sourceNode: node,
                    targetNode: aimNode
                }
                lines.push(line);
                linesMap[lineId] = line;
            }
        }
    }
}
function play() {
    //console.log('一个循环')
}

app.listen(30010);
console.log('服务启动成功，开始监听30010端口');